package app

import (
	"cloud-navigation/app/common/request"
	"cloud-navigation/app/common/response"
	"cloud-navigation/app/models"
	"cloud-navigation/app/services"
	"cloud-navigation/global"

	"github.com/gin-gonic/gin"
)

// 假日信息返回结构体
type HolidaysModel struct {
	Holidays []string `json:"holidays"`
	Works    []string `json:"works"`
}

// 获取假日信息
func Holidays(c *gin.Context) {
	var (
		holiday []string
		work    []string
		res     []models.Holiday
	)

	global.App.DB.Model(&models.Holiday{}).Find(&res)

	for _, value := range res {
		if value.Type == "holidays" {
			holiday = append(holiday, value.Date)
		} else {
			work = append(work, value.Date)
		}
	}

	holidays := HolidaysModel{
		Holidays: holiday,
		Works:    work,
	}
	response.Success(c, holidays)
}

// 添加假日信息
func HolidaysAdd(c *gin.Context) {
	var form request.CalendarAdd
	if !services.Request(c, &form) {
		return
	}
	holidays := models.Holiday{Type: form.Type, Date: form.Date}
	err := global.App.DB.Create(&holidays).Error
	if err != nil {
		response.BusinessFail(c, err.Error())
	}
	services.WriteLogs(c, "添加假日信息: type="+form.Type+"; date="+form.Date, 0)
	response.Success(c, holidays)
}
